import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.time.LocalDate;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static java.lang.Thread.sleep;


@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BlogCases extends InitAndEnd{
    public static Stream<Arguments> Generator() {
        return Stream.of(Arguments.arguments("file:///D:/Javacode/BlogSystem/blog_detail.html",
                "博客详情页",
                "我的第一篇博客"));
    }

    @Order(1)
    // 输入账号密码进行登录操作
    @ParameterizedTest
    @CsvSource({"'file:///D:/Javacode/BlogSystem/blog_login.html', 'admin', '123', 'file:///D:/Javacode/BlogSystem/blog_list.html'"})
    void Login(String url, String username, String password, String url2) {
        // 打开博客登录页面
        webDriver.get(url);
        webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        // 输入用户名
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        // 输入密码
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        // 点击提交
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        // 登录成功后，获取当前页面的url校验是否为列表页
        String cur_url = webDriver.getCurrentUrl();
        //Assertions.assertEquals(url2, cur_url);
        if (cur_url.equals(url2)) {
            // 进入正确的页面之后，校验显示的用户名是否为登录用户名
            String name = webDriver.findElement(By.cssSelector("body > div.container > div.left > div > h3")).getText();
            //Assertions.assertEquals(name, username);
            if (name.equals(username))
                System.out.println("测试通过");
            else
                System.out.println("测试2失败");
        }
        else
            System.out.println("测试1失败");
    }

    @Order(2)
    // 博客列表页数据加载正常
    @ParameterizedTest
    @CsvSource({"'file:///D:/Javacode/BlogSystem/blog_list.html'"})
    void Blog_List(String url){
        // 打开博客列表页
        webDriver.get(url);
        webDriver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        // 获取页面中的标题元素，校验是否为零
        List<WebElement> titles = webDriver.findElements(By.cssSelector("title"));
        webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        Assertions.assertNotEquals(0, titles.size());
//        if (titles.size() == 0)
//            System.out.println("测试失败");
//        else
//            System.out.println("测试通过");
    }

    @Order(3)
    // 查看全文后校验文章标题和title是否相同
    @ParameterizedTest
    @MethodSource("Generator")
    void BlogDetail(String url, String title, String name) {
        // 点击查看原文
        webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        webDriver.findElement(By.xpath("/html/body/div[2]/div[2]/div[1]/a")).click();
        // 获取页面的url
        webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        String cur_url = webDriver.getCurrentUrl();
        // 获取页面的title
        webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        String cur_title = webDriver.getTitle();
        // 获取文章标题
        webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        String cur_name = webDriver.findElement(By.cssSelector("body > div.container > div.right > div > div.title")).getText();
        webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        Assertions.assertEquals(url, cur_url);
        Assertions.assertEquals(title, cur_title);
        Assertions.assertEquals(name, cur_name);
    }

    @Order(4)
    // 写博客功能，查看写博客页面是否正确
    @ParameterizedTest
    @CsvSource({"'file:///D:/Javacode/BlogSystem/blog_edit.html', '新博客', '这是刚刚写的博客', 'file:///D:/Javacode/BlogSystem/blog_list.html'"})
    void EditBlog(String url, String title, String words, String url2){
        // 点击写博客按钮
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        // 获取页面的url是否为写博客页面
        String cur_url = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        Assertions.assertEquals(url, cur_url);
        // 输入文章标题和内容
        webDriver.findElement(By.cssSelector("#title")).sendKeys(title);
        webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        // 点击发布文章按钮
        webDriver.findElement(By.cssSelector("#submit")).click();
        webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        // 校验页面是否为文章列表页
        String cur_url2 = webDriver.getCurrentUrl();
        webDriver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
        Assertions.assertEquals(url2, cur_url2);
        // 校验第一篇文章标题是否为刚刚写的文章
        String text = webDriver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > div.title")).getText();
        String date = webDriver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > div.date")).getText();
        // 校验标题和时间是否符合预期
        Assertions.assertEquals(title, text);
        String date2 = LocalDate.now().toString();
        if (date.contains(date2))
            System.out.println("测试通过");
        else
            System.out.println("测试失败");
    }

    @Order(5)
    // 删除博客测试
    @Test
    void DeleteBlog() throws InterruptedException {
        // 打开博客列表页
        webDriver.get("file:///D:/Javacode/BlogSystem/blog_list.html");
        sleep(3000);
        // 点击查看详情
        webDriver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a")).click();
        sleep(2000);
        // 点击删除博客
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(7)")).click();
        sleep(2000);
        // 校验删除后页面是否跳转到列表页
        String url = webDriver.getCurrentUrl();
        Assertions.assertEquals("file:///D:/Javacode/BlogSystem/blog_list.html", url);
    }

    @Order(6)
    // 退出登录测试，验证点击退出登录按钮后是否跳转到登录页
    @Test
    void LogOut() throws InterruptedException {
        // 打开博客列表页
        webDriver.get("file:///D:/Javacode/BlogSystem/blog_list.html");
        // 点击退出登录
        webDriver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        sleep(2000);
        String url = "file:///D:/Javacode/BlogSystem/blog_login.html";
        sleep(3000);
        // 获取页面元素
        String username = webDriver.findElement(By.cssSelector("body > div.login-container > div > form > div:nth-child(2) > span")).getText();
        // 校验是否为登录页，页面元素是否正确
        Assertions.assertEquals("file:///D:/Javacode/BlogSystem/blog_login.html", url);
        Assertions.assertEquals("用户名", username);
    }

    @Order(7)
    // 登录失败时测试是否会提示
    @Test
    void LoginError() throws InterruptedException {
        // 打开登录页面输入未注册的用户名密码后点击登录按钮
        String username = "yly";
        String password = "a12345";
        webDriver.get("file:///D:/Javacode/BlogSystem/blog_login.html");
        sleep(3000);
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password);
        webDriver.findElement(By.cssSelector("#submit")).click();
        sleep(3000);
        // 校验元素中的文本是否为要提示的文本
        WebElement webElement = webDriver.findElement(By.cssSelector("body > h3"));
        Assertions.assertEquals( "登录失败! 用户名或者密码错误!",webElement.getText());
    }
}
